const { from } = require("zen-observable");

let arrProto = Array.prototype;


arrProto.forEach = function (cb) {
    if (typeof cb !== 'function') throw new TypeError(`${cb} is not a function`);
    let len = this.length;
    for (let i = 0; i < len; i++) {
        cb(this[i], i , this)
    }
}

arrProto.some = function (cb) {
    if (typeof cb !== 'function') throw new TypeError(`${cb} is not a function`);
    let len = this.length;
    for (let i = 0; i < len; i++) {
        if (cb(this[i], i , this)) return true;
    }
    return false;
}

arrProto.every = function (cb) {
    if (typeof cb !== 'function') throw new TypeError(`${cb} is not a function`);
    let len = this.length;
    for (let i = 0; i < len; i++) {
        if (!cb(this[i], i , this)) return false;
    }
    return true;
}

arrProto.filter = function (cb) {
    if (typeof cb !== 'function') throw new TypeError(`${cb} is not a function`);
    let len = this.length,
        newArr = [];
    for (let i = 0; i < len; i++) {
        let curItem = this[i];
        if (cb(curItem, i , this)) newArr.push(curItem);
    }
    return newArr;
}

arrProto.find = function (cb) {
    if (typeof cb !== 'function') throw new TypeError(`${cb} is not a function`);
    let len = this.length;
    for (let i = 0; i < len; i++) {
        let curItem = this[i];
        if (cb(curItem, i , this)) return curItem;
    }
}

arrProto.findIndex = function (cb) {
    if (typeof cb !== 'function') throw new TypeError(`${cb} is not a function`);
    let len = this.length;
    for (let i = 0; i < len; i++) {
        if (cb(this[i], i , this)) return i;
    }
    return -1;
}

arrProto.flat = function (deepth = 1) {
    return deepth === 0 ? this.slice(0) : this.reduce((total, now) => total.concat(Array.isArray(now) ? this.flat(now , deepth - 1) : now) , []);
}

arrProto.includes = function(targetVal, fromIndex = 0){

    let len = this.length;

    fromIndex = Number(fromIndex);

    if(fromIndex < 0) fromIndex = len + fromIndex;
    

}